package com.itheima.area.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.area.dao.FarmDao;
import com.itheima.area.pojo.CustFarm;
import com.itheima.area.service.FarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @PackageName com.itheima.area.service.impl
 * @ProjectName Ceres-Work
 * @Created by Chanly on 2020/10/11 20:27
 **/
@Service
public class FarmServiceImpl implements FarmService {
    @Autowired
    private FarmDao farmDao;

    @Override
    public List<CustFarm> findAll() {
        return farmDao.selectAll();
    }

    @Override
    public CustFarm findById(Long id) {
        return farmDao.findById(id);
    }

    @Override
    public void deleteById(Integer id) {
        farmDao.deleteByPrimaryKey(id);
    }

    @Override
    public void update(CustFarm custFarm) {
            farmDao.updateByPrimaryKey(custFarm);
    }

    @Override
    public void add(CustFarm custFarm) {
        farmDao.insert(custFarm);
    }

    @Override
    public Page<CustFarm> findPage(Map searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<CustFarm>)farmDao.selectByExample(example);
    }

    private Example createExample(Map searchMap) {
        Example example=new Example(CustFarm.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 农场所属组织
            if(searchMap.get("custid")!=null && !"".equals(searchMap.get("custid"))){
                criteria.andLike("custid","%"+searchMap.get("custid")+"%");
            }
            // 农场编号
            if(searchMap.get("code")!=null && !"".equals(searchMap.get("code"))){
                criteria.andLike("code","%"+searchMap.get("code")+"%");
            }
            // 农场名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }

            // 农场状态
            if(searchMap.get("status")!=null && !"".equals(searchMap.get("status"))){
                criteria.andEqualTo("status",searchMap.get("status"));
            }
        }
        return example;
    }

}
